package kgc.NewsServiceImpl;

import kgc.NewsDao.NewsDao;
import kgc.NewsDaoImpl.NewsDaoImpl;
import kgc.NewsService.NewsService;
import kgc.pojo.News_Category;
import kgc.pojo.News_Comment;
import kgc.pojo.News_Detail;
import kgc.pojo.News_User;
import kgc.NewsService.NewsService;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author DXH
 * @version 1.0.0
 * @ClassName NewsServiceImpl
 * @Description TODO
 * @createTime 2022年04月05日 12:36:09
 */
public class NewsServiceImpl implements NewsService {
    NewsDao newsDao = new NewsDaoImpl();

    @Override
    public List<News_Category> ServiceSelectCategoryAll() {
        List<News_Category> list = newsDao.selectCategoryAll();
        if(list.size()>0){
            return list;
        }else {
            return new ArrayList<News_Category>();
        }
    }

    @Override
    public List<News_Comment> ServiceSelectCommentAll() {
        List<News_Comment> list = newsDao.selectCommentAll();
        if(list.size()>0){
            return list;
        }else {
            return new ArrayList<News_Comment>();
        }
    }

    @Override
    public List<News_Detail> ServiceSelectDetailAll() {
        List<News_Detail> list = newsDao.selectDetailAll();
        if(list.size()>0){
            return list;
        }else {
            return new ArrayList<News_Detail>();
        }
    }

    @Override
    public List<News_User> ServiceSelectUserAll() {
        List<News_User> list = newsDao.selectUserAll();
        if(list.size()>0){
            return list;
        }else {
            return new ArrayList<News_User>();
        }
    }

    @Override
    public int ServiceDeleteById(int id, int num) {
        return newsDao.deleteById(id, num);
    }

    @Override
    public int ServiceUpdateById(int id, int num) {
        int rows = 0;
        if (num == 1){
            rows = newsDao.updateById(id, num);
        }else if (num == 2){

        }
        return rows;
    }

    @Override
    public List<Object> ServiceSelectById(int id, int num) {
        ResultSet resultSet = null;
        List<Object> list = new ArrayList<Object>();

        try {
            if (num == 1){
                resultSet = newsDao.selectById(id, num);
                News_Category category = new News_Category();
                while (resultSet.next()){
                    category.setId(resultSet.getInt(1));
                    category.setName(resultSet.getString(2));
                    category.setDate(resultSet.getTimestamp(3));
                    list.add(category);
                }
            }else if (num == 2){
                resultSet = newsDao.selectById(id, num);
                News_User user = new News_User();
                while (resultSet.next()){
                    user.setId(resultSet.getInt(1));
                    user.setUserName(resultSet.getString(2));
                    user.setPassword(resultSet.getString(3));
                    user.setEmail(resultSet.getString(4));
                    user.setUserType(resultSet.getInt(5));
                    list.add(user);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int ServiceUpdateCategory(News_Category category) {
        return newsDao.updateCategory(category);
    }

    @Override
    public int ServiceUpdateUser(News_User user) {
        return newsDao.updateUser(user);
    }

    @Override
    public int ServiceInsertCategory(News_Category category) {
        return newsDao.insertCategory(category);
    }

    @Override
    public int ServiceInsertUser(News_User user) {
        return newsDao.insertUser(user);
    }
}
